package cn.lyvust.first.manage.controller;

import cn.lyvust.first.common.annotation.SysLogs;
import cn.lyvust.first.common.bean.ResponseCode;
import cn.lyvust.first.common.bean.ResponseResult;
import cn.lyvust.first.common.exception.RequestException;
import cn.lyvust.first.common.util.BeansUtil;
import cn.lyvust.first.manage.dto.param.WangTestParam;
import cn.lyvust.first.manage.dto.param.evaluation.ItemAttrParam;
import cn.lyvust.first.manage.entity.WangTest;
import cn.lyvust.first.manage.service.WangTestService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ggzheng
 * @since 2021-02-08
 */
@Api(tags = {"wangtest"})
@RestController
@Controller
@RequestMapping("/wangtest")
  public class WangTestController {

    @Autowired
    private WangTestService wangTestService;

    @PostMapping(value = {"/list"})
    @ApiOperation(value = "查询一个表")
    public ResponseResult list(@RequestBody WangTestParam param) {
        QueryWrapper wrapper = new QueryWrapper();
        if (!StringUtils.isEmpty(param.getId())) {
            wrapper.eq("id", param.getId());
        }
        wrapper.orderByDesc("update_time");
        return ResponseResult.e(ResponseCode.OK, wangTestService.list(wrapper));

    }

    @PostMapping(value = {"/add"})
    @ApiOperation(value = "新增一个表")
    @SysLogs("新增一个表")
    public ResponseResult add(@RequestBody WangTestParam param){
        if (StringUtils.isEmpty(param.getId())) {
            throw RequestException.fail("添加失败,ID不能为空!");
        }

        if (StringUtils.isEmpty(param.getUsername())) {
            throw RequestException.fail("添加失败,姓名不能为空!");
        }
        WangTest entity = new WangTest();
        BeanUtils.copyProperties(param, entity);
        return ResponseResult.e(ResponseCode.OK, wangTestService.save(entity));
    }

    @PostMapping(value = {"/update"})
    @ApiOperation(value = "修改一个表")
    @SysLogs("修改一个表")
    public ResponseResult update(@RequestBody WangTestParam param){
        if (StringUtils.isEmpty(param.getId())) {
            throw RequestException.fail("修改失败,ID不能为空!");
        }

        WangTest entity = wangTestService.getById(param.getId());
        BeansUtil.copyPropertiesIgnoreNull(param, entity);
        return ResponseResult.e(ResponseCode.OK, wangTestService.updateById(entity));
    }

    @PostMapping(value = {"/delete"})
    @ApiOperation(value = "删除一个表")
    @SysLogs("删除一个表")
    public ResponseResult delete(@RequestBody WangTestParam param){
        if (StringUtils.isEmpty(param.getId())) {
            throw RequestException.fail("修改失败,ID不能为空!");
        }
        return ResponseResult.e(ResponseCode.OK, wangTestService.removeById(param.getId()));
    }
}